package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractProductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class ContractProductServiceImpl implements ContractProductService {

    @Autowired
    private ContractProductDao contractProductDao;

    @Autowired
    private ContractDao contractDao;

    @Autowired
    private ExtCproductDao extCproductDao;

   /*
        保存货物
    */
    public void save(ContractProduct contractProduct) {
        //赋值ID
        contractProduct.setId(UUID.randomUUID().toString());
        //计算出货物的总金额
        Double money = 0.0;
        if (contractProduct.getCnumber() != null && contractProduct.getPrice() != null){
            money = contractProduct.getCnumber() * contractProduct.getPrice();
        }
        contractProduct.setAmount(money);
        //根据合同ID查询出合同的总金额
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //获取合同金额
        Double totalAmount = contract.getTotalAmount();
        //将合同的总金额加上新增加货物的总金额
        contract.setTotalAmount(totalAmount+money);
        //将合同的货物数量加1
        contract.setProNum(contract.getProNum()+1);
        //将contractProduct数据保存
        contractProductDao.insertSelective(contractProduct);
        //修改contract数据
        contractDao.updateByPrimaryKeySelective(contract);
    }

    /*
        修改货物信息
     */
    public void update(ContractProduct contractProduct) {
        //根据当前id查询货物的旧金额
        ContractProduct oldCP = contractProductDao.selectByPrimaryKey(contractProduct.getId());
        //获取旧的总金额
        Double oldAmount = oldCP.getAmount();
        //计算当前的总金额
        Double money=0.0;
        if (contractProduct.getCnumber() != null && contractProduct.getPrice() != null) {
            money = contractProduct.getCnumber() * contractProduct.getPrice();
        }
        //赋值新的货物总金额
        contractProduct.setAmount(money);
        //根据contrcatId查询当前合同的总金额
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //赋值当前新计算出的新的合同金额 合同金额-老的货物总金额+新的货物总金额
        contract.setTotalAmount(contract.getTotalAmount()-oldAmount+money);
        //保存新的contract数据
        contractDao.updateByPrimaryKeySelective(contract);
        //保存新的货物数据
        contractProductDao.updateByPrimaryKeySelective(contractProduct);
    }

    /*
        删除货物信息
     */
    public void delete(String id) {
        //根据货物ID查询货物信息
        ContractProduct contractProduct = contractProductDao.selectByPrimaryKey(id);
        //获取货物的总金额
        Double amount = contractProduct.getAmount();
        //根据货物ID删除货物信息
        contractProductDao.deleteByPrimaryKey(id);

        ExtCproductExample example = new ExtCproductExample();
        ExtCproductExample.Criteria criteria = example.createCriteria();
        criteria.andContractProductIdEqualTo(id);
        //根据货物ID查询出货物所有的附件信息
        List<ExtCproduct> list = extCproductDao.selectByExample(example);
        //循环删除货物下的所有附件
        Double money=0.0;
        for (ExtCproduct extCproduct : list) {
            //计算所有附件的总金额
            money+=extCproduct.getAmount();
            extCproductDao.deleteByPrimaryKey(extCproduct.getId());
        }

        //根据货物ID查询合同信息
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //计算新的合同金额  合同总金额-附件总金额-货物总金额
        contract.setTotalAmount(contract.getTotalAmount()-money-amount);
        //赋值新的货物数量
        contract.setProNum(contract.getProNum()-1);
        //赋值新的附件数量
        contract.setExtNum(contract.getExtNum()-list.size());
        //将新的合同数据保存
        contractDao.updateByPrimaryKeySelective(contract);
    }

    /*
        根据合同ID查询货物信息
     */
    public ContractProduct findById(String id) {
        return contractProductDao.selectByPrimaryKey(id);
    }

    /*
        分页查询货物信息
     */
    public PageInfo findAll(ContractProductExample example, int page, int size) {
        //保存数据
        PageHelper.startPage(page,size);
        //查询所有
        List<ContractProduct> list = contractProductDao.selectByExample(example);
        return new PageInfo(list);
    }

    /*
        批量保存数据
     */
    public void saveAll(String contractId,List<ContractProduct> list) {
        //根据合同ID查询合同信息
        Contract contract = contractDao.selectByPrimaryKey(contractId);
        //修改合同货物数据
        contract.setProNum(contract.getProNum()+list.size());

        Double money=0.0;
        //遍历集合 保存数据
        for (ContractProduct contractProduct : list) {
            //设置ID
            contractProduct.setId(UUID.randomUUID().toString());
            //计算总金额
             Double amount=contractProduct.getCnumber()*contractProduct.getPrice();
             contractProduct.setAmount(amount);
            //设置总金额
            money+=amount;
            contractProductDao.insertSelective(contractProduct);
        }
        //将新的金额加入到合同金额中
        contract.setTotalAmount(contract.getTotalAmount()+money);
        //更新新的合同数据
        contractDao.updateByPrimaryKeySelective(contract);
    }
}
